<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;

/**--tmpl-use--**/

use herosphp\core\Loader;


class ManagerAction extends CommonAction {

	public function C_start()
	{
		parent::C_start();	
       // $this->order=array('updatetime'=>-1);
		$this->assign('admin_role_assoc_url','/admin/role/getAdmin_role');

	}


	protected $serviceBean="admin.adminManager.service";

	public function getAdmin_manager(HttpRequest $request) {
	
		$kw = $request->getParameter('kw');
		$request->addParameter('s_name',$kw);
		$this->index($request);
		$items = $this->getTemplateVar('items');
		$total = $this->getTemplateVar('total');
		echo json_encode(array('items' => $items,'total' => $total));
		exit;
	
	
	}
	
	public function add(HttpRequest $request) {
	
		parent::add($request);
	
		//如果不是超级管员 ，只能分配自已加的角色
		$myInfo = $this->getTemplateVar('myInfo');
		$conditions = array();
		if($myInfo['role_id'] != 0){
			$conditions['manager_id'] = $myInfo['id'];
		}
		$conditions['status'] = '1';

		$role_idoptions = Loader::model('adminRole')->getItems($conditions,null,null,array(1,10000));
		$this->assign('role_idoptions',$role_idoptions);
	
	
		$this->assign("title", "添加管理员");
		$this->assign('bread', array('系统管理',"管理员列表",$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/manager/index'),'javascript:;'));
	
	}
	
	public function edit(HttpRequest $request) {
	
	
		//如果不是超级管员 ，只能分配自已加的角色
		$myInfo = $this->getTemplateVar('myInfo');
		$conditions = array();
		if($myInfo['role_id'] != 0){
			$conditions['manager_id'] = $myInfo['id'];
		}
		$role_idoptions = Loader::model('adminRole')->getItems($conditions,null,null,array(1,10000));
		$this->assign('role_idoptions',$role_idoptions);
	
		parent::edit($request);
	
		$this->assign("title", "编辑管理员");
		$this->assign('bread', array('系统管理',"管理员列表",$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/manager/index'),'javascript:;'));
	}
	
	public function index(HttpRequest $request) {
	
		$conditions = array();
		$s_username = $request->getParameter('s_username');
		$s_mobile = $request->getParameter('s_mobile');
		$s_password = $request->getParameter('s_password');
		$s_jobno = $request->getParameter('s_jobno');
		$s_email = $request->getParameter('s_email');
		$s_name = $request->getParameter('s_name');
		$s_role_id = $request->getParameter('s_role_id');
		$s_last_login_ip = $request->getParameter('s_last_login_ip');
		$s_status = $request->getParameter('s_status');
		$start_time = $request->getParameter('start_time');
		$end_time = $request->getParameter('end_time');
		$select_mix_option2 = $request->getParameter('select_mix_option2');

		$select_mix_option1 = $request->getParameter('select_mix_option1');

		$select_mix_option_value1 = $request->getParameter('select_mix_option_value1');

		if($s_last_login_ip !== '' && $s_last_login_ip!== NULL)$conditions['last_login_ip'] = '%'.$s_last_login_ip.'%';
		if($s_role_id !== '' && $s_role_id!== NULL)$conditions['role_id'] = $s_role_id;
		if($s_status !== '' && $s_status!== NULL)$conditions['status'] = '%'.$s_status.'%';

		if($select_mix_option_value1!='') {

				switch($select_mix_option1){

					case 'username':$conditions['username'] = '%' . $select_mix_option_value1 . '%';break;
					case 'jobno':$conditions['jobno'] = '%' . $select_mix_option_value1 . '%';break;
					case 'email':$conditions['email'] = '%' . $select_mix_option_value1 . '%';break;
					case 'name':$conditions['name'] = '%' . $select_mix_option_value1 . '%';break;
					case 'mobile':$conditions['mobile'] = '%' . $select_mix_option_value1 . '%';break;

				}

		}


        if($select_mix_option2=='add_time'){
			if($start_time !== '' && $start_time!== NULL){
				$conditions['addtime'] = array();
				$conditions['addtime']['>'] = $start_time.' 00:00:00';
			}

			if($end_time !== '' && $end_time!== NULL){
				if(!$conditions['addtime']){ $conditions['addtime'] = array();}
				$conditions['addtime']['<='] = $end_time.' 23:59:59';
			}

		}elseif($select_mix_option2=='last_login_time'){

			if($start_time !== '' && $start_time!== NULL){
				$conditions['last_login_time'] = array();
				$conditions['last_login_time']['>'] = $start_time.' 00:00:00';
			}

			if($end_time !== '' && $end_time!== NULL){
				if(!$conditions['last_login_time']){ $conditions['last_login_time'] = array();}
				$conditions['last_login_time']['<='] = $end_time.' 23:59:59';
			}

		}


		$this->assign('_table','管理员表');
		$this->assign('s_username',$s_username);
		$this->assign('s_password',$s_password);
		$this->assign('s_mobile',$s_mobile);
		$this->assign('s_jobno',$s_jobno);
		$this->assign('s_email',$s_email);
		$this->assign('s_name',$s_name);
		$this->assign('s_role_id',$s_role_id);
		$this->assign('s_last_login_ip',$s_last_login_ip);
		$this->assign('s_status',$s_status);

		$this->assign('_filter',array('username','mobile','jobno','email','name','role_name','last_login_time','last_login_ip','status','manager_name','addtime','updatetime'));
		$this->assign('_header',array('用户名','手机号码','工号','电子邮箱','姓名','角色','最后登陆时间','最后登陆IP','状态','添加人','添加时间','更新时间'));
		$this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
		$this->assign('select_mix_option1',$select_mix_option1);
		$this->assign('select_mix_option2',$select_mix_option2);
		$this->assign('select_mix_option_value1',$select_mix_option_value1);



		//如果不是超级管理员则只能看到自已添加的管理员
		$myInfo = $this->getTemplateVar('myInfo');
		if($myInfo['role_id'] != 0){
			$conditions['manager_id'] = $myInfo['id'];
		}

		//导出，add By Lao Liu
        if($request->getParameter('export') == 1){
            $this -> setPage(1);
            $this -> setPagesize(2147483648);
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $conditions['id']['$in'] = $ids;
            }
        }
		$this->setConditions($conditions);
		$this->order=array('updatetime'=>-1);
	
		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);
		$this->setPagesize($pagesize);
		//$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		
		parent::index($request);
	
		/**--tmpl-indexafter--**/
	
		$role_idoptions = Loader::model('adminManagerRole')->getItems();

        if($request->getParameter('export') == 1){
            $items = $this -> getTemplateVar('items');
            $filter = $this->getTemplateVar('_filter');
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                foreach($filter as $kk => $vv)
                {
                    if( $vv == 'role_name' ) {
                        $tmp[] = $v[$vv] ? $v[$vv] : "超级管理员";
                    } elseif ($vv == 'status'){
                        $tmp[] = $v[$vv]==0 ? "禁用" : "启用";
                    } elseif ($vv == 'last_login_ip' ){
                        $tmp[] = long2ip($v[$vv]);
                    } else{
                        $tmp[] = $v[$vv];
                    }

                }
                $excel[] = $tmp;
            }
            phpexcel($this->getTemplateVar('_table'),$this->getTemplateVar('_header'),$excel);
            exit();
        }
        $total = $this->getTemplateVar('total');
        $this->generatePageMenu($total);
		$this->assign('role_idoptions',$role_idoptions);
	
		$this->assign("title", "管理员列表");
		$this->assign('bread', array('系统管理',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
	}
	
	public function do_add(HttpRequest $request)
	{
		$data = $request->getParameter('data');
		if($data['password']!=$data['re_password']){

			AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,L('MANAGER_PASSWORD_ATYPISM'));

		}
		$data['password'] = passwd($data['password']);
		//添加管理员的管理员
		$data['manager_id'] = adminId();
	
		$data['last_login_ip'] = ip2long($data['last_login_ip']);
		if(!$data['status']){$data['status'] = 0;}
		$service = Beans::get($this->getServiceBean());




		if($service->count(array('username' => trim($data['username']))) >= 1)
		{
			AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,L('MANAGER_USERNAME_UNIQUE'));
		}

		if($data['mobile']!=''&&$service->count(array('mobile' => trim($data['mobile']))) >= 1)
		{
			AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,L('MANAGER_MOBILE_UNIQUE'));
		}
	
		$request->addParameter('data',$data);
		parent::do_add($request);	
	}
	
	public function do_edit(HttpRequest $request){
		$data = $request->getParameter('data');
		$myInfo = $this->getTemplateVar('myInfo');
	

		if($data['password'])
		{
			if($data['password']!=$data['re_password']){

				AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,'两次密码不一致');

			}

			$data['password'] = passwd($data['password']);
		}
		else{
			unset($data['password']);
		}
		if(!$data['status']){$data['status'] = 0;}
		$data['manager_id'] = adminId(); 
		$service = Beans::get($this->getServiceBean());
		if(isset($data['username']) && $service->count(array('username' => trim($data['username']),'id' => array('!=' => $request->getParameter('id')))) >= 1)
		{
			AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,L('MANAGER_USERNAME_UNIQUE'));
		}
		if(isset($data['mobile']) && $service->count(array('mobile' => trim($data['mobile']),'id' => array('!=' => $request->getParameter('id')))) >= 1)
		{
			AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,L('MANAGER_MOBILE_UNIQUE'));
		}
		$data['session_id'] = '';
		$request->addParameter('data',$data);
		parent::do_edit($request);	
	}

	//修改个人信息
	public function editSelfInfo(HttpRequest $request)
	{
		$id = adminId();
		$service = Beans::get($this->getServiceBean());

		$myInfo = $this->getTemplateVar('myInfo');
		$data = $request->getParameter('data');
		if($data['password'])
		{
			if(passwd($data['orgpwd']) != $myInfo['password'])
			{
				AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,L('ORG_PASSWORD_ERROR'));
			}
			else if($data['password'] != $data['confirm'])
			{
				AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,L('PASSWORD_CONFIRM_ERROR'));
			}
			$data['password'] = passwd($data['password']);
			$is_del = true;
		}
		else{
			unset($data['password']);
		}
		unset($data['confirm']);
		unset($data['orgpwd']);


		if($service->update($data,$id)){
			$service->updateMyInfo($data);
			if($is_del) $service->logout();
			AjaxResult::ajaxSuccessResult();
		}
		AjaxResult::ajaxFailtureResult();

		
	}
	/**
	 * 禁用、启用多条数据
	 * @param HttpRequest $request
	 */
	public function do_updateStatus( HttpRequest $request ) {

		$ids = $request->getParameter('ids');
		$type = $request->getParameter('type');

		if ( empty($ids) ) AjaxResult::ajaxResult('error', COM_ERR_MSG);

		$service = Beans::get($this->getServiceBean());

		if($type==1){
			$bool=$service->updates(array('status'=>1),array('id' => array('$in' => $ids) ));

		}else{
			$bool=$service->updates(array('status'=>0),array('id' => array('$in' => $ids) ));

		}

		if ($bool ) {
			AjaxResult::ajaxSuccessResult();
		} else {
			AjaxResult::ajaxFailtureResult();
		}
	}


}
